$(document).ready(function() {
	setTimeout("init();",500) ;
}) ;

function init(){
	$("#memberLayoutAll").extcloudTabChange($("#memberLayoutAll .ui-tabs-nav > li"),$("#changeUser > ul") ,null) ;
	var treeChoose = $.fn.zTree.getZTreeObj("choose");
	var treechoosedte = treeChoose.getNodes();
	$.fn.zTree.init($("#choose"), settingchoose ,treechoosedte);
}

var treeNodes;
var zTreeObj;
var setting = {  
		async:{
			enable :false
		},
		check:{
			chkStyle:"checkbox",
			chkboxType  :{ "Y" : "s", "N" : "s" },  // 点击复选框节点之间的选择
			enable: true
			
		},
		view:{
			showIcon: true ,// 是否显示节点图标
			expandSpeed: "slow" // 节点展开效果
		},
		callback: {
			onRightClick: onRightClick , // 屏蔽右键
			beforeDrag: beforeDrag,
			beforeDrop: beforeDropRealize
			
		
		},
		edit: {
			enable: true ,
			showRemoveBtn: false,
			showRenameBtn: false
			
		}
	}; 

var settingchoose = {  
		async:{
			enable :false
		},
		view:{
			showLine: false,
			showIcon: true ,
			expandSpeed: "slow" 
		},
		callback: {
			onRightClick: onRightClick ,
			onDblClick: beforeRemove
		},
		edit: {
			enable: true ,
			showRenameBtn : false,
			removeTitle: "点击删除节点"
		}
	};  

var mycars=new Array("ec_current_unit_defaults_tree","ec_department_tree","ec_possessions_all_tree") ;
function onChoose(){
	var treeChoose = $.fn.zTree.getZTreeObj("choose");
	for ( var j = 0; j < mycars.length; j++) {
		var treeObj = $.fn.zTree.getZTreeObj(mycars[j]);
		if(treeObj != null){
			var nodes = treeObj.getCheckedNodes(true);
			if(mycars[j]=="ec_department_tree"){
				for(var i = 0 ; i < nodes.length ; i ++) {
					var nodesvalue=nodes[i].id;
					var nodesChoose = treeChoose.getNodesByParam("id", nodesvalue, null);
					if(nodesChoose=="" ){
						var treenodesdate = nodes[i] ;
						var treechildren = treenodesdate.children ;
						if(treechildren==""){
							treeChoose.addNodes(null, treenodesdate);
						}
					}
				}
			}else{
				for(var i = 0 ; i < nodes.length ; i ++) {
					var nodesvalue=nodes[i].id;
					var nodesChoose = treeChoose.getNodesByParam("id", nodesvalue, null);
					if(nodesChoose=="" && nodesvalue!=null ){
						treeChoose.addNodes(null, nodes[i]);
					}
				}
			}
		}
	}
}

function onRightClick(event, treeId, treeNode) {

};

function beforeDrag(treeId, treeNodes) {
	var treeChoose = $.fn.zTree.getZTreeObj("choose");
	for(var i = 0 ; i < treeNodes.length ; i ++) {
		if(treeId=="ec_department_tree"){
			var nodes = treeChoose.getNodesByParam("id", treeNodes[i].id, null);
			if(nodes=="" ){
				var treenodesdate = treeNodes[i] ;
				var treechildren = treenodesdate.children ;
				treenodesdate.children=null ;
				if(treenodesdate.isParent){
					treenodesdate.isParent=false ;
				}
				treeChoose.addNodes(null, treenodesdate);
				if(!treenodesdate.isParent){
					treenodesdate.isParent=true ;
					treenodesdate.children =treechildren ;
				}
			}
		}else{
			var treenodesdate = treeNodes[i] ;
			var treechildren = treenodesdate.children ;
			if(treechildren==null){
				var nodes = treeChoose.getNodesByParam("id", treenodesdate.id, null);
				if(nodes=="" ){
					treeChoose.addNodes(null, treenodesdate);
				}
			}else{
				gettrees(treechildren ,treeChoose) 
			}
		}
   }
	return true;
}
function gettrees(treeNodes ,treeChoose) {
	for(var j = 0 ; j < treeNodes.length ; j ++){
		var treechildren = treeNodes[j].children ;
		if(treechildren==null){
			var nodes = treeChoose.getNodesByParam("id", treeNodes[j].id, null);
			if(nodes=="" ){
				treeChoose.addNodes(null, treeNodes[j]);
			}
		}else{
			gettrees(treechildren,treeChoose) ;
		}
	}
}
function beforeDropRealize(treeId, treeNodes) {
	return false;
}

function beforeRemove() {
	var treeRemove = $.fn.zTree.getZTreeObj("choose");
	var selectnode = treeRemove.getSelectedNodes();
	for (var i=0, l=selectnode.length; i < l; i++) {
		treeRemove.removeNode(selectnode[i]);
	}
}

function deleteAll(){
	$.fn.zTree.init($("#choose"), settingchoose, "");
}
var ec_department_tree ,ec_current_unit_defaults_tree,ec_possessions_all_tree ;
function loaddate(type){
	var typedate;
	var cutout = type.split("_") 
	ec_type="&type="+cutout[cutout.length-2] ;
	if(ec_department_tree!=null && type=="ec_department_tree" ){
		$.fn.zTree.init($("#"+type), setting , ec_department_tree);
	}else if(ec_current_unit_defaults_tree!=null && type=="ec_current_unit_defaults_tree" ){
		$.fn.zTree.init($("#"+type), setting , ec_current_unit_defaults_tree);
	}else if(ec_possessions_all_tree!=null && type=="ec_possessions_all_tree" ){
		$.fn.zTree.init($("#"+type), setting , ec_possessions_all_tree);
	}else {
		$.ajax({  
		       async : false,  
		       cache:false,  
		       type: 'POST',  
		       data: ec_type+"&loadingDataType=synchro" ,  // all 为全部 department 部门 defaults 当前部门
		       dataType : "json",  
		       url: "ecTreeAction.do?method=getZTreedata",
		       error: function () { 
		           alert('数据加载失败！');  
		       },  
		       success:function(data){  
		    	   if(type=="ec_department_tree"){
		    		   ec_department_tree = data; 
		    	   }
		    	   if(type=="ec_current_unit_defaults_tree"){
		    		   ec_current_unit_defaults_tree= data;  
		    	   }
		    	   if(type=="ec_possessions_all_tree"){
		    		   ec_possessions_all_tree= data;  
		    	   }
		    	   $.fn.zTree.init($("#"+type), setting , data);
		       }  
		   }); 
		
	}
}



